package com.editoken.mp.controller;

import com.editoken.mp.domain.dto.PageDTO;
import com.editoken.mp.domain.dto.UserFormDTO;
import com.editoken.mp.domain.po.User;
import com.editoken.mp.domain.query.UserQuery;
import com.editoken.mp.domain.vo.UserVO;
import com.editoken.mp.service.IUserService;
import com.fasterxml.jackson.databind.util.BeanUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理接口")
@RequestMapping("/users")
@RestController
@RequiredArgsConstructor // lombok
public class UserController {
    private final IUserService userService;

    @ApiOperation("新增用户接口")
    @PostMapping
    public boolean saveUser(@ApiParam("用户表单数据") @RequestBody UserFormDTO userDTO) {
        User user = new User();
        user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());
        user.setBalance(userDTO.getBalance());
        user.setInfo(userDTO.getInfo());
        user.setPhone(userDTO.getPhone());
        user.setId(userDTO.getId());
        return userService.save(user);
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("/{id}")
    public void deleteUser(@ApiParam("用户id") @PathVariable("id") int id) {

        userService.removeById(id);
    }

    @ApiOperation("查询用户接口")
    @GetMapping("/{id}")
    public User queryUser(@ApiParam("用户id") @PathVariable("id") int id) {

         return userService.getById(id);
    }
    @ApiOperation("查询多个用户")
    @GetMapping
    public List<User> queryUserByIds(@ApiParam("用户id集合") @RequestParam("idList") List<Long> idList) {

        return userService.listByIds(idList);
    }

    @ApiOperation("根据id扣减余额")
    @PutMapping("/{id}/deduction/{money}")
    public void  deductMoneyById(
            @ApiParam("用户id") @PathVariable("id") Long id,
            @ApiParam("扣减的金额") @PathVariable("money") Integer money
    ){
        userService.deductMoneyById(id, money);
    }


    @ApiOperation("根据复杂条件查询用户接口")
    @GetMapping("/list")
    public  List<User> queryUsers(UserQuery query) {

        return userService.queryUsers(query);
    }

    @ApiOperation("分页查询用户")
    @GetMapping("/page")
    public PageDTO<User> queryUserPage(UserQuery query){

        return userService.queryUsersPage(query);
    }

}
